package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Comment;
import cn.ztydata.teachingsystem.heaven.entity.User;
import cn.ztydata.teachingsystem.heaven.service.CommentService;
import cn.ztydata.teachingsystem.heaven.web.dto.speakComment.CommentDto;
import cn.ztydata.teachingsystem.heaven.web.wrapper.HttpSessionProxy;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.MessageFormat;

/**
 * 发言回复控制类
 *
 * @author lyj
 * @since 2015-3-20
 */
@Controller
@RequestMapping("/reply")
public class CommentController extends BaseController {
    @Autowired
    private CommentService commentService;
    @Value("${session.key}")
    private String sessionKey;


    //<editor-fold desc="新增">
    /**
     * 新增回复
     *
     * @param commentDto 发言实体
     * @return JsonResult
     * @author lyj
     * @since 2015-3-21
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addSpeak(@Valid @RequestBody CommentDto commentDto) {

        commentService.addComment(mapper.map(commentDto, Comment.class));

        log.debug("新增回复");
        return new JsonResult(true, "新增回复成功！");
    }
    //</editor-fold>


    //<editor-fold desc="删除">
    /**
     * 删除回复
     *
     * @param id 回复ID
     * @return JsonResult
     * @author lyj
     * @since 2015-3-23
     */
    @RequestMapping(value = "/delete/{id}/{talkId}/{groupType}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteComment(@PathVariable("id") long id,
                                    @PathVariable("talkId") long speakId,
                                    @PathVariable("groupType") int groupType,
                                    HttpServletRequest request) {
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "回复Id"));
        Assert.isTrue(speakId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "发言Id"));
        Assert.isTrue(groupType > 0 && groupType < 4, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组类型"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        commentService.deleteComment(id, speakId, groupType, userInfo.getNumber());

        log.debug("删除回复");
        return new JsonResult(true, "删除成功！");
    }
    //</editor-fold>


    /**
     * 获取回复列表
     *
     * @param speakId 发言ID
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @return
     * @author lyj
     * @since 2015-3-23
     */
    @RequestMapping(value = "/list/{speakId}/{groupType}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getList(@PathVariable("speakId") long speakId,
                                  @RequestParam("page") int pageNum,
                                  @RequestParam("pageSize") int pageSize,
                                  @PathVariable("groupType") int groupType){
        Assert.isTrue(speakId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "发言ID"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));
        Assert.isTrue(groupType > 0 && groupType < 4, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组类型"));

        Page<Comment> page = commentService.getList(speakId, pageNum, pageSize, groupType);

        log.debug("获取回复列表");
        return new JsonResultPage(new Page(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), CommentDto.class)));

    }
}
